package com.apexsoft.monitor.front.console.app.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.monitor.front.console.aop.AopAnnotation;
import com.apexsoft.monitor.front.console.pageModel.Papp;
import com.apexsoft.front.common.support.BaseFrontController;
import com.apexsoft.front.common.utils.annotation.CrudMethod;
import com.apexsoft.monitor.front.console.app.service.AppService;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller("appController")
@RequestMapping("/console/app")
public class AppController extends BaseFrontController {
	
	@Autowired
	@Qualifier("appService")
	private AppService appService = null;
	

	/**
	 * 节点列表(分页查询)
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/listByPage")
	public String listByPage(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "console/app/appList";
		
	}
	
	@RequestMapping(value = "/loadAppByPage")
	public String getEsbNodes(HttpServletRequest request, HttpServletResponse response,@ModelAttribute("form") Papp papp) throws Exception {
		Pagination page =new Pagination(papp.getPageNum(),papp.getPageSize());
		JSONObject result = this.appService.listByPage(page,papp);
		this.printJSON(response, result);
		return null;
		
	}
	@RequestMapping(value = "/getAllApp")
	public String getAllApp(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONArray array = new JSONArray();
		JSONObject result = appService.getAllApp("1");
		if (result != null && result.getIntValue(Result.CODE) > 0) {
			JSONObject nullObj = new JSONObject();
			nullObj.put("appCode","");
			nullObj.put("appName","----请选择----");
			array.add(nullObj);
			array.addAll(1,result.getJSONArray(Result.ROWS));
		}
		this.printJSON(response, array);
		return null;

	}
	@CrudMethod
	@AopAnnotation(description="添加应用")
	@RequestMapping(value = "/add")
	public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		JSONObject result = appService.add(request);
		this.printJSON(response, result);
		return null;
		
	}

	@CrudMethod
	@AopAnnotation(description="修改应用信息")
	@RequestMapping(value = "/update")
	public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		JSONObject result = appService.update(request);
		this.printJSON(response, result);
		return null;
		
	}

	@CrudMethod
	@AopAnnotation(description="根据id删除节点")
	@RequestMapping(value = "/delete")
	public String delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		JSONObject result = appService.delete(id);
		this.printJSON(response, result);
		return null;
		
	}
	
}
